#region Summary

// 
// Owner : JackieHan 
// 
// Solution : DailyCollection 
// 
// Project : Jackie.Utility.Notify
// 
// File : Service.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 服务主体
// 
// History : 
//			2011-04-25 14:53:50	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using Common.Logging;
using Jackie.Utility.CustomException;
using Jackie.Utility.Notify.Contracts;
using Jackie.Utility.Notify.Domain;
using Jackie.Utility.Notify.Services.Manager;

#endregion

namespace Jackie.Utility.Notify.Services
{
    /// <summary> 
    /// 服务主体
    /// </summary>
    public class PublishService : IPublish
    {
        private readonly ILog logger;
        private readonly TitleManager titleManager;
        private readonly MessageManager messageManager;

        public PublishService()
        {
            logger = LogManager.GetLogger(GetType());
            titleManager = TitleManager.GetInstance();
            messageManager = MessageManager.GetInstance();
        }

        public void SendMessage(MessageInfo info)
        {
            try
            {
                logger.Info("start send message:" + info.ToString());
                messageManager.SendMessage(info);
                logger.Info("end send message!");
            }
            catch (Exception ex)
            {
                logger.Info("error!", ex);
            }

        }

        public void RegisteredTitle(TitleInfo info)
        {
            try
            {
                logger.Info("start register message title:" + info.ToString());
                titleManager.PublishTitle(info);
                logger.Info("end register message title!");
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }

        public void CancelTitle(string titleCode)
        {
            try
            {
                logger.Info("start cancel message title! titleCode:" + titleCode);
                titleManager.CancelTitle(titleCode);
                logger.Info("end cancel message title!");
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }

        }

        public IEnumerable<TitleInfo> GetAllTitles()
        {
            logger.Info("start get all titles!");
            var titles = titleManager.GetAllTitles();
            logger.Info("end get!");
            return titles;
        }


        public bool IsTitleExisted(string titleCode)
        {
            logger.Info(string.Format("start judgement the titleCode[{0}] exists!", titleCode));
            var exist = titleManager.IsTitleExisted(titleCode);
            logger.Info("end judgement!the result is " + exist);
            return exist;
        }

        public TitleInfo Get(string titleCode)
        {
            logger.Info(string.Format("start to get title by titleCode[{0}]", titleCode));
            try
            {
                 var title = titleManager.GetTitle(titleCode);
                 logger.Info(string.Format("end to get! the result is {0}", title.ToString()));
                 return title;
            }
            catch (CodeNotExistedException ex)
            {
                logger.Error("get title error", ex);
            }
            return null;
        }
    }
}